package com.uchiha.nova.mainService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.uchiha.nova.core.model.entity.ChinaOrgCodeEntity;
import com.uchiha.nova.core.model.vo.ChinaOrgCodeVO;
import com.uchiha.nova.mainService.mapper.ChinaOrgCodeMapper;
import com.uchiha.nova.mainService.service.IChinaOrgCodeService;
import com.uchiha.nova.mainService.wrapper.ChinaOrgCodeWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 中国区域机构代码 服务实现类
 *
 * @author Uchihashiamu
 */
@Service
public class ChinaOrgCodeServiceImpl extends ServiceImpl<ChinaOrgCodeMapper, ChinaOrgCodeEntity> implements IChinaOrgCodeService {
    @Override
    public List<ChinaOrgCodeVO> tree(Long id) {
        List<ChinaOrgCodeVO> all = new ArrayList<>();
        ChinaOrgCodeEntity entity = this.getById(id);
        List<ChinaOrgCodeEntity> list = this.list(new QueryWrapper<ChinaOrgCodeEntity>().eq("parent_id", id));
        list.add(entity);
        List<ChinaOrgCodeVO> res = ChinaOrgCodeWrapper.build().listNodeVO(list);

        return res;
    }

    @Override
    public List<ChinaOrgCodeVO> treeStation(Long id) {
        List<ChinaOrgCodeEntity> all = new ArrayList<>();
        ChinaOrgCodeEntity entity = this.getById(id);
        ChinaOrgCodeEntity parent = this.getById(entity.getParentId());
        List<ChinaOrgCodeEntity> list = this.list(new QueryWrapper<ChinaOrgCodeEntity>().eq("parent_id", id));
        all.add(entity);
        all.add(parent);
        all.addAll(list);
        list.forEach(entity1 -> {
            List<ChinaOrgCodeEntity> listc = this.list(new QueryWrapper<ChinaOrgCodeEntity>().eq("parent_id", entity1.getId()));
            all.addAll(listc);
        });
        List<ChinaOrgCodeVO> res = ChinaOrgCodeWrapper.build().listNodeVO(all);
        return res;
    }

    @Override
    public List<ChinaOrgCodeVO> treeRole(Long id) {
        List<ChinaOrgCodeEntity> all = new ArrayList<>();
        ChinaOrgCodeEntity entity = this.getById(id);
        ChinaOrgCodeEntity cityEntity = this.getById("370600000000");
        List<ChinaOrgCodeEntity> areaList = this.list(new QueryWrapper<ChinaOrgCodeEntity>().eq("parent_id", "370600000000"));
        all.add(entity);
        all.add(cityEntity);
        all.addAll(areaList);
        List<ChinaOrgCodeVO> res = ChinaOrgCodeWrapper.build().listNodeVO(all);
        return res;
    }

    @Override
    public List<ChinaOrgCodeVO> treeData(String dataAuth) {
        ChinaOrgCodeEntity dataEntity = this.getById(dataAuth);
        List<ChinaOrgCodeEntity> all = new ArrayList<>();
        if(dataEntity.getLevel()==1){
            all.add(dataEntity);
            ChinaOrgCodeEntity cityEntity = this.getById("370600000000");
            all.add(cityEntity);
            List<ChinaOrgCodeEntity> areaList = this.list(new QueryWrapper<ChinaOrgCodeEntity>().eq("parent_id", "370600000000"));
            all.addAll(areaList);
        }else if(dataEntity.getLevel()==2){
            List<ChinaOrgCodeEntity> chinaOrgCodeEntities = this.list(new QueryWrapper<ChinaOrgCodeEntity>().eq("parent_id", dataEntity.getId()));
            all.add(dataEntity);
            all.addAll(chinaOrgCodeEntities);
        }else if(dataEntity.getLevel()==3){
            all.add(dataEntity);
        }
        List<ChinaOrgCodeVO> res = ChinaOrgCodeWrapper.build().listNodeVO(all);
        return res;
    }
}
